﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class frmAdminLogin : System.Web.UI.Page
{
    #region EVENT MODULES

    protected void Page_Load(object sender, EventArgs e)
    {
        //### Logout User
        if (Request.QueryString["action"] == "syslogout")
        {
            //### Clear all session variables
            Session.Clear();
        }

        //Check if there is a Web Site Login Request
        if (Request.QueryString["FrontLogin"] == "True")
        {
            //Begin Login
            //Check Details if they where submitted correctly
            if (Request.QueryString["Username"] == "" || Request.QueryString["Username"] == null || Request.QueryString["Password"] == "" || Request.QueryString["Password"] == null)
            {
                //Respond Back with an error
                Response.Redirect("MonstromPets_MyAccount.aspx?Error=Credentials");
            }
            else
            {
                //### Check login
                LoginUser(Request.QueryString["Username"].ToString(), Request.QueryString["Password"].ToString());
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        bool bCanSave = true;

        //### Validate email
        if (txtEmailAddress.Text == "")
        {
            txtEmailAddress.Attributes.Add("style", "border:1px solid #911f22");
            lblMessage.Text = "<b class=\"errorMsg\">Please enter your email address.</b>";
            bCanSave = false;
        }

        //### Validate password
        if (txtPassword.Text == "")
        {
            txtPassword.Attributes.Add("style", "border:1px solid #911f22");
            lblMessage.Text = "<b class=\"errorMsg\">Please enter your password.</b>";
            bCanSave = false;
        }

        //## If both blank
        if (txtEmailAddress.Text == "" && txtPassword.Text == "")
        {
            lblMessage.Text = "<b class=\"errorMsg\">Please enter your email and password.</b>";
            bCanSave = false;
        }

        //### If valid
        if (bCanSave == true)
        {
            //### Check login
            LoginUser();
        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        Response.Redirect("admin/frmConsole.aspx");
        txtEmailAddress.Text = "";
        txtPassword.Text = "";
        lblMessage.Text = "";

        txtEmailAddress.Attributes.Add("style", "border:1px solid #FFF");
        txtPassword.Attributes.Add("style", "border:1px solid #FFF");

    }

    #endregion

    #region LOGIN METHODS

    private void LoginUserX()
    {
        //### This function will check the user logging in is a valid user in the DB
        //### Check username and password is valid
        if (txtEmailAddress.Text != "" && txtPassword.Text != "")
        {
            lblMessage.Text = "<b class=\"errorImg\"><img src=\"images/valid.png\" /> Successful login :)";
            txtEmailAddress.Attributes.Add("style", "border:1px solid #0076ec");
            txtPassword.Attributes.Add("style", "border:1px solid #0076ec");

            Response.Redirect("admin/frmConsole.aspx");
        }
    }

    private void LoginUser()
    {
        clsCommonFunctions clsCommonFunctions = new clsCommonFunctions();
        clsUser clsUser = null;

   
            //### Hash the password
           // string strHashPassword = clsCommonFunctions.GetMd5Sum(txtPassword.Text);

            //### Validate that the user logging in is a registered user, 
            //### or write error to lblLoginMsg
            try
            {
                clsUser = new clsUser(txtEmailAddress.Text, txtPassword.Text);

                //### Set Session["clsUser"]
                Session["clsUser"] = clsUser;

                //### Redirect to console page
                Response.Redirect("admin/frmConsole.aspx");


            }
            catch (Exception ex)
            {
                if (ex.Message.StartsWith("Thread") == true)
                {
                    Response.Redirect("admin/frmConsole.aspx");
                }
                else
                lblMessage.Text = "<b class=\"errorMsg\">Invalid login credentials! Please try again.</b>";
            }
        

    }

    private void LoginUser(string strUsername, string strPassword)
    {
        clsCommonFunctions clsCommonFunctions = new clsCommonFunctions();
        clsUser clsUser = null;


        //### Hash the password
        //string strHashPassword = clsCommonFunctions.GetMd5Sum(strPassword);

        //### Validate that the user logging in is a registered user, 
        //### or write error to lblLoginMsg
        try
        {
            clsUser = new clsUser(strUsername, strPassword);

            if (clsUser.bStaffMember == true)
            {
                //### Set Session["clsUser"]
                Session["clsUser"] = clsUser;
                //### Redirect to console page
                Response.Redirect("admin/frmConsole.aspx");
            }
            else
            {
                //### Set Session["clsUser"]
                Session["clsClient"] = clsUser;
                Response.Redirect("MonstromPets_MyAccount.aspx?Status=LoggedIn");
            }


        }
        catch (Exception ex)
        {
            if (ex.Message.StartsWith("Thread") == true)
            {

                if (clsUser.bStaffMember == true)
                {
                    //### Set Session["clsUser"]
                    Session["clsUser"] = clsUser;
                    //### Redirect to console page
                    Response.Redirect("admin/frmConsole.aspx");
                }
                else
                {
                    //### Set Session["clsUser"]
                    Session["clsClient"] = clsUser;
                    Response.Redirect("MonstromPets_MyAccount.aspx?Status=LoggedIn");
                }
            }
            else
                //Respond Back with an error
                Response.Redirect("MonstromPets_MyAccount.aspx?Error=Credentials");
        }


    }

    #endregion
}
